cmake_minimum_required(VERSION 3.7...4.0)

project(scream LANGUAGES C)

add_executable(${PROJECT_NAME} scream.c network.c shmem.c raw.c)

# find pulseaudio
option(PULSEAUDIO_ENABLE "Enable PulseAudio" ON)
if (PULSEAUDIO_ENABLE)
  find_package(PkgConfig)
  pkg_check_modules(PULSEAUDIO libpulse-simple)
  if (PULSEAUDIO_FOUND)
    include_directories(${PULSEAUDIO_INCLUDE_DIRS})
    target_link_directories(${PROJECT_NAME} PUBLIC ${PULSEAUDIO_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} ${PULSEAUDIO_LIBRARIES})
    target_sources(${PROJECT_NAME} PRIVATE pulseaudio.c)
  else ()
    set(PULSEAUDIO_ENABLE OFF)
  endif ()
endif ()

# find ALSA
option(ALSA_ENABLE "Enable ALSA" ON)
if (ALSA_ENABLE)
  find_package(PkgConfig)
  pkg_check_modules(PC_ALSA alsa)
  if (PC_ALSA_FOUND)
    include_directories(${PC_ALSA_INCLUDEDIR})
    target_link_directories(${PROJECT_NAME} PUBLIC ${PC_ALSA_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} ${PC_ALSA_LIBRARIES})
    target_sources(${PROJECT_NAME} PRIVATE alsa.c)
  else ()
    set(ALSA_ENABLE OFF)
  endif ()
endif ()

# find JACK
option(JACK_ENABLE "Enable JACK" ON)
if (JACK_ENABLE)
  find_package(PkgConfig)
  pkg_check_modules(PC_JACK jack)
  pkg_check_modules(PC_LIBSOXR soxr)
  if(PC_JACK_FOUND AND PC_LIBSOXR_FOUND)
    include_directories(${PC_JACK_INCLUDEDIR} ${PC_LIBSOXR_INCLUDEDIR})
    target_link_directories(${PROJECT_NAME} PUBLIC ${PC_JACK_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} ${PC_JACK_LIBRARIES} ${PC_LIBSOXR_LIBRARIES})
    target_sources(${PROJECT_NAME} PRIVATE jack.c)
  else ()
    set(JACK_ENABLE OFF)
  endif ()
endif ()


# find libpcap
option(PCAP_ENABLE "Enable PCAP" ON)
if (PCAP_ENABLE)
  find_package(PkgConfig)
  pkg_check_modules(PC_PCAP libpcap)
  if (PC_PCAP_FOUND)
    include_directories(${PC_PCAP_INCLUDEDIR})
    target_link_directories(${PROJECT_NAME} PUBLIC ${PC_PCAP_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} ${PC_PCAP_LIBRARIES})
    target_sources(${PROJECT_NAME} PRIVATE pcap.c)
  else ()
    set(PCAP_ENABLE OFF)
  endif ()
endif ()

option(SNDIO_ENABLE "Enable sndio" ON)
if (SNDIO_ENABLE)
  find_package(PkgConfig)
  pkg_check_modules(PC_SNDIO sndio)
  if (PC_SNDIO_FOUND)
    include_directories(${PC_SNDIO_INCLUDEDIR})
    target_link_directories(${PROJECT_NAME} PUBLIC ${PC_SNDIO_LIBRARY_DIRS})
    target_link_libraries(${PROJECT_NAME} ${PC_SNDIO_LIBRARIES})
    target_sources(${PROJECT_NAME} PRIVATE sndio.c)
  else ()
    set(SNDIO_ENABLE OFF)
  endif ()
endif ()

configure_file(config.h.in config.h)
target_include_directories(${PROJECT_NAME} PUBLIC "${PROJECT_BINARY_DIR}")

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
